#coding:gbk

from smartcard import *
from sct import *

COUNT = 8 * 1024
INS = 0xAB

md5_list = []
sha1_list = []
sha224_list = []
sha256_list = []
sha384_list = []
sha512_list = []

def write_file(hash_list, p1, fileName):
    with open(fileName, 'w') as fObj:
        for in_data, out_data in hash_list:
            in_length = len(in_data)
            if in_length < 0x100:
                cmd_apdu = '\x80\xAB%s\x00%s%s' %(chr(p1), chr(in_length), in_data)
            else:
                cmd_apdu = '\x80\xAB%s\x00\x00%s%s%s' %(chr(p1), chr(in_length >> 8), chr(in_length & 0xFF), in_data)
            fObj.write("//expect:" + s(out_data, '', '', '') + "9000")
            fObj.write('\n')
            fObj.write(s(cmd_apdu) + ";")
            fObj.write('\n\n')

def main():
    mplib_init()
    data = ''
    for i in range(COUNT):
        in_data = ''.join([chr(x & 0xFF) for x in range(i)])
        in_data = mplib_lshift('\x01', i)
        md5_out_data        = mplib_hash(HASH_MD5, in_data)
        sha1_out_data       = mplib_hash(HASH_SHA1, in_data)
        sha224_out_data     = mplib_hash(HASH_SHA224, in_data)
        sha256_out_data     = mplib_hash(HASH_SHA256, in_data)
        sha384_out_data     = mplib_hash(HASH_SHA384, in_data)
        sha512_out_data     = mplib_hash(HASH_SHA512, in_data)

        md5_list.append((in_data, md5_out_data))
        sha1_list.append((in_data, sha1_out_data))
        sha224_list.append((in_data,  sha224_out_data))
        sha256_list.append((in_data, sha256_out_data))
        sha384_list.append((in_data, sha384_out_data))
        sha512_list.append((in_data, sha512_out_data))

        if(i % 0x100 == 0):
            print i

    write_file(md5_list, HASH_MD5, "md5.scr")
    write_file(sha1_list, HASH_SHA1, "sha1.scr")
    write_file(sha224_list, HASH_SHA224, "sha224.scr")
    write_file(sha256_list, HASH_SHA256, "sha256.scr")
    write_file(sha384_list, HASH_SHA384, "sha384.scr")
    write_file(sha512_list, HASH_SHA512, "sha512.scr")

if __name__ == '__main__':
    main()

